﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CSSAdriverapp
{
    /// <summary>
    /// Interaction logic for UpdateActivity.xaml
    /// </summary>
    public partial class UpdateActivity : Window
    {
        public UpdateActivity()
        {
            InitializeComponent();

            DataClassesDataContext db = new DataClassesDataContext();

            errorLabel.Content = getTerm();

            IQueryable<COMSERV_ACTIVITY> activities = from c in db.COMSERV_ACTIVITies
                                                      where c.semester_yearid == getTermID()
                                                      select c;
            activities.ToList(); //executes and puts into list
            activityDataGrid.ItemsSource = activities;

            IQueryable<Vehicle> vehicles = from d in db.Vehicles select d;
            vehicles.ToList();
            vehicleDataGrid.ItemsSource = vehicles;
        }

        private static string getTerm()
        {
            DataClassesDataContext db = new DataClassesDataContext();

            int term = getTermID();

            if (term != 0)
            {

                IQueryable<COMSERV_SEMESTER_YEAR> info = from c in db.COMSERV_SEMESTER_YEARs
                                                         where c.semester_yearid == (int)term
                                                         select c;
                info.ToList();
                return info.First().semester + " " + info.First().year;
            }
            return "Please generate events first!";
        }

        private static int getTermID()
        {
            DataClassesDataContext db = new DataClassesDataContext();

            try
            {
                int id = (int)(from e in db.Events
                               join a in db.COMSERV_ACTIVITies on (int)e.ServiceID equals (int)a.activityid
                               select a.semester_yearid).FirstOrDefault();
                return id;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return 0;
        }

        public List<COMSERV_ACTIVITY> activities
        {
            get;
            set;
        }

        public List<Vehicle> vehicles
        {
            get;
            set;
        }

        private void activityDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void vehicleDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (vehicleDataGrid.SelectedItem != null)
            {
                if (activityDataGrid.SelectedItem != null)
                {
                    //Message box code
                    string activity = ((COMSERV_ACTIVITY)(activityDataGrid.SelectedItem)).activity_name;
                    string vehicle = ((Vehicle)(vehicleDataGrid.SelectedItem)).Name;
                    string message = "This will assign " + activity + " with " + vehicle ;
                    string caption = "Vehicle";
                    MessageBoxResult result = MessageBox.Show(message, caption,
                                             MessageBoxButton.OK,
                                             MessageBoxImage.Question);
                }
            }
            else
            {
                //Message box code
                string message = "Select an activity";
                string caption = "Vehicle";
                MessageBoxResult result = MessageBox.Show(message, caption,
                                         MessageBoxButton.OK,
                                         MessageBoxImage.Question);
            }
        }

        private void updateActivityButton_Click(object sender, RoutedEventArgs e)
        {
            if (activityDataGrid.SelectedItem != null && vehicleDataGrid.SelectedItem != null)
            {
                DataClassesDataContext db = new DataClassesDataContext();
                int vehicleid = ((Vehicle)(vehicleDataGrid.SelectedItem)).ID;
                IQueryable<Event> events = from c in db.Events where c.ServiceID == ((COMSERV_ACTIVITY)(activityDataGrid.SelectedItem)).activityid select c;

                List<Event> eventslist = events.ToList();

                foreach (Event d in eventslist)
                {
                    assignVehicle(d, vehicleid);
                }


                string message = "Activity Updated";
                string caption = "Activity";
                MessageBoxResult result = MessageBox.Show(message, caption,
                                         MessageBoxButton.OK,
                                         MessageBoxImage.Information);

                this.Close();
            }
            else
            {
                string message = "Please select both an activity and a vehicle!";
                string caption = "Activity";
                MessageBoxResult result = MessageBox.Show(message, caption,
                                         MessageBoxButton.OK,
                                         MessageBoxImage.Exclamation);
            }
        }

        private void assignVehicle(Event e, int v)
        {
            DataClassesDataContext db = new DataClassesDataContext();
            // Query the database for the row to be updated.
            var query =
                from ev in db.Events
                where ev.ID == e.ID
                select ev;
            // Execute the query, and change the column values
            // you want to change.
            foreach (Event ev in query)
            {
                ev.VehicleID = v;
                // Insert any additional changes to column values.
            }

            // Submit the changes to the database.
            try
            {
                db.SubmitChanges();
            }
            catch (Exception w)
            {
                Console.WriteLine(w);
                // Provide for exceptions.
            }
        }


    }
}
